[MLAS] Integrate Q4 asymmetric KleidiAI kernels into MatMulNBits#29654
[MLAS] Integrate Q4 asymmetric KleidiAI kernels into MatMulNBits#29654martin-klacer-arm wants to merge 4 commits into
Conversation
* Added asymmetric path to MatMulNBits Q4 using asymmetric kernels Signed-off-by: Martin Klacer <martin.klacer@arm.com>
Signed-off-by: Martin Klacer <martin.klacer@arm.com>
Signed-off-by: Martin Klacer <martin.klacer@arm.com>
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
There was a problem hiding this comment.
Pull request overview
This PR extends the MLAS/KleidiAI-backed MatMulNBits CPU path to support asymmetric Q4 (Int4) weights for SQNBIT_CompInt8 by selecting new KleidiAI asymmetric kernels and ensuring zero-points are handled correctly during prepack and shared-prepack reuse.
Changes:
- Adds KleidiAI asymmetric Q4 backend selection/packing in MLAS, plus dispatch support (alignment + “needs ZP correction” capability queries).
- Updates
MatMulNBitsprepack/sharing behavior to (a) pass constant zero-points into packing when available, and (b) decline prepack for runtime zero-points on the KleidiAI packed path to avoid incorrect sharing. - Adds targeted unit tests for KleidiAI asymmetric Q4 and for dynamic zero-point + shared-prepack negative cases.
Reviewed changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| onnxruntime/test/mlas/unittest/test_sqnbitgemm.cpp | Plumbs backend selector config through test harness; adds KleidiAI asymmetric Q4 CompInt8 coverage. |
| onnxruntime/test/contrib_ops/matmul_nbits_prepack_sharing_test_util.h | Adds a sharing-mode that expects prepack to be declined. |
| onnxruntime/test/contrib_ops/matmul_nbits_prepack_sharing_test_util.cc | Implements “expect no prepack” assertions for the new sharing-mode. |
| onnxruntime/test/contrib_ops/matmul_4bits_test.cc | Adds dynamic zero-point tests and shared-prepack negative-control coverage for KleidiAI asymmetric Q4 CompInt8. |
| onnxruntime/core/mlas/lib/sqnbitgemm_kernel_neon_int8.cpp | Adds asymmetric backend selection and packed-kernel dispatch plumbing for ARM64/KleidiAI Q4. |
| onnxruntime/core/mlas/lib/qnbitgemm.h | Extends MLAS QNBit dispatch ABI for packed Q4 CompInt8 (alignment + ZP-correction capability + kernel config). |
| onnxruntime/core/mlas/lib/qnbitgemm.cpp | Uses new dispatch hooks to select alignment/correction behavior and pass backend config through packed paths. |
| onnxruntime/core/mlas/lib/qnbitgemm_kernel_neon.h | Declares new ARM64/KleidiAI Q4 backend selection helpers and packed-path hooks. |
| onnxruntime/core/mlas/lib/qnbitgemm_kernel_neon.cpp | Implements asymmetric Q4 RHS packing and backend selection for KleidiAI; wires new dispatch pointers. |
| onnxruntime/core/mlas/lib/kai_ukernel_interface.h | Adds asymmetric Q4 ukernel wrapper types/layout metadata and accessors. |
| onnxruntime/core/mlas/lib/kai_ukernel_interface.cpp | Registers/selects KleidiAI asymmetric Q4 ukernels (dot/i8mm/SME2 variants) and exposes accessors. |
| onnxruntime/contrib_ops/cpu/quantization/matmul_nbits.cc | Updates prepack/sharing logic for constant-vs-runtime zero-points and for “finalized packed B” semantics on the KleidiAI packed path. |
|
Thank you for the review comments. They seem sensible, I'll upload a patch with fixes as soon as it's ready. |
* Changed params structs to zero-initialization in qnbitgemm_kernel_neon.cpp SQ4BitGemmPackQuantBDataAndBlkSum * Changed temporary memory allocation in qnbitgemm_kernel_neon.cpp (zero_offsets, rhs_for_kai) to reused nr-wide panel scratch buffer Signed-off-by: Martin Klacer <martin.klacer@arm.com>
|
Thank you for the comments. I addressed the 'params' concerns as suggested by zero-initializing the variables. To address the memory concerns, I replaced the full-N temporary buffers with a reused nr-wide panel scratch buffer. Measuring with |
Review: PR #29654 — [MLAS] Integrate Q4 asymmetric KleidiAI kernels into MatMulNBitsApprove. Introduces a dedicated KleidiAI backend ( What looks correct
Non-blocking observations
SummarySubstantial and well-motivated integration of the KleidiAI asymmetric Q4 kernels. Correctness rests on a single |
|
Thanks you for this nice PR and for addressing the previous set of comments.
|
Description
This PR integrates KleidiAI asymmetric Q4 kernels into the
MatMulNBitspath forSQNBIT_CompInt8when zero points are available during prepack.The operator-facing changes are limited to the KleidiAI-eligible packed path:
MatMulNBitsnow passes constant zero points into MLAS prepacking, marks shared prepacked buffers as finalized only when scales/zero points are actually folded into the packed B buffer, and declines prepacking for runtime zero-point inputs for the KleidiAI path so those cases keep using the existing fallback behavior.Within MLAS, this adds asymmetric Q4 backend selection, RHS/LHS packing for the KleidiAI asymmetric packed layout, and dispatch for the corresponding GEMV/GEMM kernels. Cases that can't use the native asymmetric path continue to use the existing symmetric packed path and zero-point correction behavior.
Motivation and Context
Asymmetric block-quantized Q4 previously didn't have a dedicated execution path using KleidiAI - it was routed through the shared symmetric Q4 path with an additional zero-point correction step.
This PR adds a path that processes asymmetric zero points directly, allowing supported prepacked asymmetric weights to use KleidiAI kernels that handle zero points natively.
The performance was tested on a Mac Mini with M4 Pro using clean Release builds and
onnxruntime_perf_test -m times -x 1 -s -S 1. The tables report the mean across 5 outer benchmark command invocations, each running 20 timed inferences for the E2E model (100 total) and 1000 timed inferences for synthetic nodes (5000 total).Most of the uplift provided is thanks to the newly integrated asymmetric kernels using SME2 if available, and the asymmetric/symmetric split leaves room for future integration into either path.
The primary performance uplift was observed on an end-to-end test with the Phi 3.5 Mini Instruct ONNX model, using the CPU Int4 AWQ Block-128 variant.
Additional uplift was observed on synthetic MatMulNBits cases using various shapes derived from Qwen 2.5 1.5B Instruct AWQ Block-128 Int4 asymmetric.
In addition, the measured symmetric control model exercising the existing symmetric path was unchanged as expected.